package com.cooba.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;
/**
 * Created by Administrator on 2015/12/20.
 * 新闻详情
 */
public class NewsDetailsActivity extends AppCompatActivity {
    private WebView web;
    private String replyid, title;
    private WebSettings settings;
    private HttpUtils httpUtils;
    private RequestParams params;
    private TextView wb_title;
    private final static String HEAD = "<h3 style=text-align:center></h3><head><style>img{max-width:100%%;height:auto; !important;}</style></head>";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details);
        web = (WebView) findViewById(R.id.web_News_Details);
        wb_title = (TextView) findViewById(R.id.wb_title);
        settings = web.getSettings();
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setJavaScriptEnabled(true);

   /*     settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //优先使用缓存
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);*/
        Intent intent = getIntent();
        replyid = intent.getStringExtra("newsDetialreplyid");
        title = intent.getStringExtra("newsDetialtitle");
        init(replyid);
        wb_title.setText(title);
    }

    private void init(String replyid) {
        httpUtils = new HttpUtils();
        params = new RequestParams();
        params.addBodyParameter("id", replyid);
        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.newsDetail, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("sss", "失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                JSONObject json = null;
                try {
                    json = new JSONObject(responseInfo.result);
                    String content = json.get("content").toString().replaceAll("\\\\", "");
                    web.loadDataWithBaseURL(null, HEAD + content, "text/html", "utf-8", null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    // 设置回退
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
